package com.justcs.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.justcs.entity.*;
import com.justcs.form.*;
import com.justcs.mapper.*;
import com.justcs.view.ConfOrderView;
import com.justcs.view.MeetingRoomView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 会议预定相关服务
 */
@Service
public class ConfOrderService {

    @Autowired
    private MeetingRoomMapper meetingRoomMapper;

    @Autowired
    private UserinfoMapper userinfoMapper;

    @Autowired
    private MeetingCollectMapper meetingCollectMapper;

    @Autowired
    private IssueLastmeetMapper issueLastmeetMapper;

    @Autowired
    private IssueCurmeetMapper issueCurmeetMapper;

    @Autowired
    private SuggestionMapper suggestionMapper;

    @Autowired
    private ConferenceMapper conferenceMapper;

    @Autowired
    private ConfAttrMapper confAttrMapper;

    @Autowired
    private AttendesMapper attendesMapper;

    @Autowired
    private ConfMeetingRoomMapper confMeetingRoomMapper;

    /**
     * 分页查询会议室相关信息
     *
     * @param query
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public PageInfo<MeetingRoomView> queryPagedMeetingRoom(PagedQueryForm<MeetingRoom_c> query) {
        if (query != null) {
            PageHelper.startPage(query.getPage(), query.getPagesize());
            PageInfo<MeetingRoomView> meetingRoomPageInfo = new PageInfo<>(
                    meetingRoomMapper.queryAllMeetingRoom(
                            query.getSearch(),
                            query.getSort(),
                            query.getOrder()
                    )
            );
            return meetingRoomPageInfo;
        }
        return null;
    }


    /**
     * 分页查询用户信息
     *
     * @param query
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public PageInfo<UserSelectView> queryUserSelect(PagedQueryForm<UserSelectView_c> query) {
        if (query != null) {
            PageHelper.startPage(query.getPage(), query.getPagesize());
            PageInfo<UserSelectView> userSelectViewPageInfo = new PageInfo<>(
                    userinfoMapper.selectUsrSelect(
                            query.getSearch().getDepid(),
                            query.getSearch().getPositionid(),
                            query.getSearch().getDutyid()
                    )
            );
            return userSelectViewPageInfo;
        }
        return null;
    }


    /**
     * 查询待选择用户的信息
     *
     * @param param
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<UserSelectView> queryAllUsrSelect(UserSelectView_c param) {
        if (param != null) {
            return userinfoMapper.selectUsrSelect(param.getDepid(), param.getPositionid(), param.getDutyid());
        }
        return null;
    }

    /**
     * 会议预约表单录入
     *
     * @param param
     * @return
     */
    @Transactional
    public boolean addConfOrderInfo(ConfOrderView param) {
        if (param != null) {
            // 1.录入会议议题
            MeetingCollect meetingCollect = param.getMeetingCollect();
            meetingCollectMapper.insertSelective(meetingCollect);

            // 2. 录入上次上次会议议题
            issueLastmeetMapper.batchInsert(param.getIssueLastmeetList(), meetingCollect.getId());

            // 3. 本次会议议题
            issueCurmeetMapper.batchInsert(param.getIssueCurmeetList(), meetingCollect.getId());

            // 4.建议解决方案
            suggestionMapper.batchInsert(param.getSuggestionList(), meetingCollect.getId());

            // 5.录入会议
            ConferenceWithBLOBs conference = param.getConference();
            conference.setMeetcollectid(meetingCollect.getId());
            conferenceMapper.insertSelective(conference);

            // 6.录入会议的属性
            confAttrMapper.batchInsert(param.getConfAttributesList(), conference.getId());

            // 7.列席人员
            attendesMapper.batchInsert(param.getAttendesList(), conference.getId());

            // 8.会议室占用表
            ConfMeetingRoom confMeetingRoom = param.getConfMeetingRoom();
            confMeetingRoom.setConfid(conference.getId());
            confMeetingRoomMapper.insertSelective(param.getConfMeetingRoom());

            return true;
        }
        return false;
    }


    /**
     * 新增会议室
     *
     * @param meetingRoom
     * @return
     */
    @Transactional
    public MeetingRoom addMeetingRoom(MeetingRoom meetingRoom) {
        if (meetingRoom != null) {
            meetingRoomMapper.insertSelective(meetingRoom);
            return meetingRoom;
        }
        return null;
    }

}
